<style>
    .layui-form-label {
        width: 100px;
    }
    .layui-input-block {
        margin-left: 130px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">计量方案名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入计量方案名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">计量方案编码：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入计量方案编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="productPackBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="productPackBtnDel" class="layui-btn icon-btn layui-btn-danger">批量删除</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="productPackTable" lay-filter="productPackTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="productPackTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="productPackForm">
    <form lay-filter="productPackForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">包裝计量名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入包装计量名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物料类型</label>
            <div class="layui-input-block">
                <select id="productType" name="productType" lay-filter="productType" lay-verify="required" lay-search >
                    <option value=""> 请选择物料类型</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物料单位</label>
            <div class="layui-input-block">
                <select id="productUnitId" name="productUnitId" lay-filter="productUnitId" lay-verify="required" lay-search >
                    <option value=""> 请选择物料单位</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">换算方式</label>
            <div class="layui-input-block">
                <select name="rateType">
                        <option value="fixed">固定换算率</option>
                        <option value="floating">浮动</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">换算率</label>
            <div class="layui-input-block">
                <input name="baseRate" placeholder="请输入换算率" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea  name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>

        <!--<div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>-->

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="productPackFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#productPackTable',
            url: config.base + 'enterprise/product/admin/productPack/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'name', sort: true, title: '包装计量方案名称', fixed: 'left', width: 150}
                ,{field: 'code', sort: true, title: '包装计量方案编码', width: 150}
                ,{field: 'productUnitName', title: '物料单位'}
                ,{field: 'productTypeName', title: '所属物料类型'}
                ,{field: 'isBaseUnit', sort: true, title: '是否基础单位',templet:function (d) {
                        if(d.isBaseUnit){
                            return '是'
                        }else{
                            return  '否'
                        }
                    }}
                ,{field: 'baseRate', sort: true, title: '换算率'}
                ,{field: 'converteMethod', sort: true, title: '换算公式'}
                // ,{field: 'rateTypeDesc', sort: true, title: '换算方式'}



                ,{field: 'isDeleted', sort: true, title: '是否启用',templet:function (d) {
                        if(d.isDeleted){
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }

                    }}
                ,{field: 'creatorName', sort: true, title: '创建人'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#productPackTableBar', title: '操作', width: 200, fixed: 'right'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(productPackTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(productPackTable)', function(obj){
            table.reload('productPackTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('enterprise/product/admin/productPack/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productPackTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('enterprise/product/admin/productPack/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productPackTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 添加按钮点击事件
        $('#productPackBtnAdd').click(function () {
            showEditModel();
        });

        $('#productPackBtnDel').click(function () {
            var checkRows = table.checkStatus('productPackTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
            } else {
                var ids='';
                checkRows.data.forEach(item=>{
                    ids+=item.id+','
                })
                layer.confirm('确定要删除吗？', {
                    offset: '65px',
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    delBatch(ids)
                });
            }
        });

        function delBatch(ids){
            admin.req('enterprise/product/admin/productPack/delBatch', {ids:ids}, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productPackTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '760px',
                offset: '65px',
                title: data ? '修改物料包装' : '添加物料包装',
                content: $('#productPackForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='rateType']").find("option");
                        layui.each(option, function(index, item){
                            if(data.rateType  == item.text){
                                data.rateType = item.value;
                            }
                        });
                    }

                    admin.req('enterprise/product/admin/productType/list', {limit:9999}, function (res) {
                        if ( res.code==200) {
                            var html='<option value=""> 请选择物料类型</option>';
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-productTypeCode="'+res.data[k].code+'">'+res.data[k].name+'</option>';
                            }
                            $("form select[name='productType']").html(html);
                            form.render('select')
                            if(data){
                                $("form select[name='productType']").val(data.productType);
                                getProductUnit(data.productType,false);
                            }
                        } else {
                            layer.msg('获取物料类型失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    var getProductUnit=function(productType,reSelect){
                        admin.req('enterprise/product/admin/productUnit/list', {productType:productType,limit:9999}, function (res) {
                            if ( res.code==200) {
                                var html='<option value=""> 请选择物料单位</option>';
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-productUnitCode="'+res.data[k].code+'">'+res.data[k].name+'</option>';
                                }
                                $("form select[name='productUnitId']").html(html);
                                form.render('select')
                                if(data&&!reSelect){
                                    $("form select[name='productUnitId']").val(data.productUnitId);
                                    form.render()
                                }
                            } else {
                                layer.msg('获取物料类型失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    form.on('select(productType)',function(data){
                        if(data.value!=''){
                            getProductUnit(data.value,true);
                        }
                    })

                    form.val('productPackForm', data);
                    // 表单提交事件
                    form.on('submit(productPackFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        d.field.productTypeCode=$("form select[name='productType'] option:selected").data('producttypecode');
                        d.field.productUnitCode=$("form select[name='productUnitId'] option:selected").data('productunitcode');
                        admin.req(data ? 'enterprise/product/admin/productPack/update' : 'enterprise/product/admin/productPack/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('productPackTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/product/admin/productPack/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#productPackBtnExport').click(function () {
            var checkRows = table.checkStatus('productPackTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>